
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.BizRule;
using Swaf.Security;

namespace Syrinx
{
	public class FileSysBizRules : Syrinx.BaseBizRules
	{
		public IBizObjList getFileSysCaches(IApplication app, IRuleContext context)
		{
			context.put("companyId", ActiveCompanyId);
			IBizObjList caches = app.appData.get("DA.FileSys.GetCompanyCaches", context) as IBizObjList;
			return caches;
		}

		public void updateFileSysCache(IApplication app, IRuleContext context, IBizObj cacheInfo)
		{
			if (cacheInfo.isNew)
				app.appData.put("DA.FileSys.AddCompanyCache", cacheInfo);
			else
				app.appData.put("DA.FileSys.UpdateCompanyCache", cacheInfo);
		}

		public void deleteFileSysCache(IApplication app, IRuleContext context, string cacheName)
		{
			context.put("cacheName", cacheName);
			context.put("companyId", ActiveCompanyId);
			app.appData.put("DA.FileSys.DeleteCompanyCache", context);
		}

		//--------------------------------------------------------------------------------------------

		public IBizObjList getFileSysDirectories(IApplication app, IRuleContext context, IBizObj info)
		{
			string startPath = app.resMgr.resolve(Cmn.pathCombine("$BaseFileSysStore$", "$CmpSetting(sitePath)$"));
			IBizObjList directories = app.bizObjMgr.create("FileSysDirectory");
			getChildDirectories(startPath, directories, startPath.Length, info.get(""), 0);
			return directories;
		}

		public IBizObj getFileSysDirectoryDetails(IApplication app, IRuleContext context, IBizObj info)
		{
			return null;
		}

		public void updateFileSysDirectory(IApplication app, IRuleContext context, IBizObj dirInfo)
		{
		}

		public void deleteFileSysDirectory(IApplication app, IRuleContext context, IBizObj dirInfo)
		{
		}

		protected IBizObjList getChildDirectories(string path, IBizObjList list, int pos, string cacheName, int curDepth)
		{
			DirectoryInfo di = new DirectoryInfo(path);
			curDepth++;
			foreach (DirectoryInfo childDir in di.GetDirectories())
			{
				IBizObj directory = list.insertNew();
				Swaf.BGuid.Guid pid = new Swaf.BGuid.Guid();
				string keyName = childDir.FullName.Substring(pos).Replace(Path.DirectorySeparatorChar, ':');
				int index = keyName.LastIndexOf(':');
				if (index != -1)
					directory.put("parentName", keyName.Substring(0, index));
				else
					directory.put("parentName", null);
				directory.put("name", childDir.Name);
				directory.put("keyName", keyName);
				directory.put("depth", curDepth);
				directory.put("cacheName", cacheName);
				directory.put("fileCount", childDir.GetFiles().Length);
				directory.clearDirtyFlags();
				directory.isNew = false;
				getChildDirectories(childDir.FullName, list, pos, cacheName, curDepth);
			}
			return list;
		}

		//--------------------------------------------------------------------------------------------
		
		public IBizObjList getFileSysFiles(IApplication app, IRuleContext context, IBizObj info)
		{
			DirectoryInfo di = new DirectoryInfo(Cmn.pathCombine("$BaseFileSysStore$", "$CmpSetting(sitePath)$"), info.get("baseDirectory").ToString()));
			return null;
		}

		public IBizObj getFileSysFileDetails(IApplication app, IRuleContext context, IBizObj info)
		{
			return null;
		}

		public void updateFileSysFile(IApplication app, IRuleContext context, IBizObj file)
		{
		}

		public void deleteFileSysFile(IApplication app, IRuleContext context, IBizObj fileInfo)
		{
		}


	}
}
